package net.test.service.impl;

import javax.annotation.Resource;

import net.test.bean.Pager;
import net.test.dao.AdminDao;
import net.test.entity.Admin;
import net.test.service.AdminService;

import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("adminServiceImpl")
public class AdminServiceImpl extends BaseServiceImpl<Admin, String> implements AdminService{

	@Resource(name="adminDaoImpl")
	private AdminDao adminDao;
	@Resource(name="adminDaoImpl")
	public void setBaseDao(AdminDao adminDao){
		super.setBaseDao(adminDao);
	}
	
	@Override
	public Admin getLoginAdmin() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			return null;
		}
		Object principal = authentication.getPrincipal();
		if (principal == null || !(principal instanceof Admin)) {
			return null;
		} else {
			return (Admin) principal;
		}
	}
	
	@Transactional(readOnly=true)
	public Admin loadLoginAdmin(){
		Admin admin=getLoginAdmin();
		if(admin==null){
			return null;
		}else{
			return adminDao.load(admin.getId());
		}
	}

	@Transactional(readOnly = true)
	public boolean isExistByUsername(String username) {
		return adminDao.isExistByUsername(username);
	}
	
	@Override
	public Admin getAdminByUsername(String username) {
		
		return adminDao.getAdminByUsername(username);
	}

	@Override
	public Pager findPagerByAdmin(Pager pager, Admin admin) {
	
		return adminDao.findPagerByAdmin(pager,admin);
	}

}
